package com.jingjiu.homeservicesbiz.controller;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingjiu.homeservicesapi.domain.ForumPost;
import com.jingjiu.homeservicesbiz.service.ForumPostServiceImpl;

import com.jingjiu.homeservicesapi.util.R;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 论坛帖子：记录用户发布的内容Controller
 * 
 * @author ruoyi
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/system/post")
public class ForumPostController
{

    @Autowired
    private ForumPostServiceImpl forumPostService;



  /*  @GetMapping("/page")
    public R<Page<ForumPost>> getPage(@ParameterObject Page<ForumPost> page,
                                      @ParameterObject ForumPost forumPost) {


        // 调用 Service 层进行分页查询
        return R.ok(forumPostService.selectPage(page, forumPost));
    }*/
    /**
     * 查询论坛帖子：记录用户发布的内容列表
     */
    @GetMapping("/page")
    @ResponseBody
    public R getList(){
        return R.ok(forumPostService.selectForumPostList());
    }



    /**
     * 导出论坛帖子：记录用户发布的内容列表
     */
/*
    @PostMapping("/list")
    @ResponseBody
    public R<List<ForumPost>> list(@RequestBody ForumPost forumPost)
    {
        return R.ok(forumPostService.selectForumPostList(forumPost));

    }*/



    /**
     * 新增保存论坛帖子：记录用户发布的内容
     */

    @PostMapping("/add")
    @ResponseBody
    public R addSave(@RequestBody ForumPost forumPost)
    {
        int row = forumPostService.insertForumPost(forumPost);
        if(row!=1)
            return R.failed();
        return R.ok(row);
    }


    /**
     * 按id获取
     *
     * @param id   本我

     * @return {@link ForumPost }
     */

    @GetMapping("/get/{id}")
    public R<ForumPost> getById(@PathVariable("id") Long id)
    {
        ForumPost forumPost = forumPostService.selectForumPostById(id);
        if(forumPost==null)
            return R.failed();
         return R.ok(forumPost);

    }
    @GetMapping("/search")
    public R<List<ForumPost>> searchPostsByTitle(@RequestParam("title") String title) {
        List<ForumPost> forumPosts = forumPostService.searchByTitle(title);
        if(forumPosts==null)
            return R.failed();
        return R.ok(forumPosts);
    }
    /**
     * 修改保存论坛帖子：记录用户发布的内容
     */

    @PostMapping("/edit")
    @ResponseBody
    public R editSave(@RequestBody ForumPost forumPost)
    {
        int row = forumPostService.updateForumPost(forumPost);
        if(row!=1)
            return R.failed();
        return R.ok(row);
    }

    /**
     * 删除论坛帖子：记录用户发布的内容
     */

    @PostMapping( "/remove")
    @ResponseBody
    public R  remove(Long id)
    {

        int row = forumPostService.deleteForumPostById(id);
        if(row!=1)
            return R.failed();
        return R.ok(row);
    }
}
